<?php
    include '../connectdb.php';
    if (isset($_POST['action'])) {
        $q = "update booking_detail set status_id=".$_POST['status']." where booking_id=".$_POST['booking_id'];
        mysqli_query($link, $q);
        $dt = '';
        if ($_POST['status'] == 2) {
            $dt = "deposit_date='".date('Y-m-d')."', ";
        } else {
            if ($_POST['status'] == 2 && $_POST['hdTotal'] == $_POST['deposit']) {
                $dt = "complete_payment_date='".date('Y-m-d G:i:s')."', ";
            }
        }
        $q = "update booking set deposit=".$_POST['deposit'].", ".$dt." last_update='".date('Y-m-d G:i:s')."' where booking_id=".$_POST['booking_id'];
        mysqli_query($link, $q);
    }
    if (isset($_GET["edit"])) {
        $q = "select booking.*, customer.customer_fname, customer.customer_lname from booking, customer where booking.customer_id=customer.customer_id and booking.booking_id=".$_GET['booking_id'];
        $result = mysqli_query($link, $q);
        $row = mysqli_fetch_array($result);
        $total = $row['total_price'];
        $deposit = $row['deposit'];
        $remain = $total - $deposit;
        $q = "select min(booking_detail.date) as mindate, max(booking_detail.date) as maxdate, status_id from booking_detail where booking_id=".$_GET['booking_id'];
        $res = mysqli_query($link, $q);
        $r = mysqli_fetch_array($res);
        $url = "main.php?booking=true";//&edit=true&customer_id=".$_GET["customer_id"];
        $action = 'edit';
        $cod = date ("Y-m-d", strtotime("+1 day", strtotime($r['maxdate'])));
        $status = $r['status_id'];
        $q = "select * from status";
        $res2 = mysqli_query($link, $q);
        $st = array();
        while ($st[] = mysqli_fetch_array($res2));
        array_pop($st);
?>
<form action="<?php echo $url ?>" method="POST">
<input type="hidden" name="action" value="<?php echo $action ?>" />
<input type="hidden" name="booking_id" value="<?php echo $_GET["booking_id"] ?>" />
<table border="1" width="764">
    <tr>
        <td colspan="2" align="center"><h3>Booking Management</h3></td>
    </tr>
    <tr>
        <td>id</td>
        <td><?php echo $row['booking_id'] ?></td>
    </tr>
    <tr>
        <td>Customer</td>
        <td><?php echo $row['customer_fname'].' '.$row['customer_lname'] ?></td>
    </tr>
    <tr>
        <td>Booking Date</td>
        <td><?php echo $row['booking_date'] ?></td>
    </tr>
    <tr>
        <td>Check-in</td>
        <td><?php echo $r['mindate'] ?></td>
    </tr>
    <tr>
        <td>Check-out</td>
        <td><?php echo $cod ?></td>
    </tr>
    <tr>
        <td>Total</td>
        <td><?php echo $total ?><input type="hidden" name="hdTotal" id="hdTotal" value="<?php echo $total ?>"></td>
    </tr>
    <tr>
        <td>Paid</td>
        <td><input type="text" value="<?php echo $deposit ?>" name="deposit" /></td>
    </tr>
    <tr>
        <td>Remain</td>
        <td><?php echo $remain ?></td>
    </tr>
    <tr>
        <td>Paid Date</td>
        <td><?php echo $row['deposit_date'] ?></td>
    </tr>
    <tr>
        <td>status</td>
        <td>
            <select name="status">
                <?php
                    foreach ($st as $key => $val) {
                        $selected = ($val["status_id"] == $status) ? "selected" : "" ;
                ?>
                <option value="<?php echo $val["status_id"] ?>" <?php echo $selected ?>><?php echo $val["status_name"] ?></option>
                <?php
                    }
                ?>
            </select>
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center"><input type="submit" value="submit" /> <input type="reset" value="reset"/></td>
    </tr>
</table>
</form>
<?php
    } else {
        $q = "select * from status";
        $res2 = mysqli_query($link, $q);
        $st = array();
        while ($st[] = mysqli_fetch_array($res2));
        array_pop($st);
        $_st = $_GET['status_id'];
        $_st = ($_st == '') ? 1 : $_st;
?>
<table border="1" width="764">
    <tr>
        <td colspan="11" align="center"><h3>Booking Management</h3></td>
    </tr>
    <tr>
        <td colspan="11">
            <select id="filter" onchange="window.location.href='main.php?booking=true&status_id='+this.value;">
<?php
            foreach ($st as $key => $val) {
                $selected = ($val["status_id"] == $_st) ? 'selected' : '';
?>
                <option value="<?php echo $val["status_id"] ?>" <?php echo $selected ?>><?php echo $val["status_name"] ?></option>
<?php
            }
?>
            </select>
        </td>
    </tr>
    <tr>
        <td>id</td>
        <td>Customer</td>
        <td>Booking Date</td>
        <td>Check-in</td>
        <td>Check-out</td>
        <td>Total</td>
        <td>Paid</td>
        <td>Paid Date</td>
        <td>Remain</td>
        <td>Status</td>
        <td>edit</td>
    </tr>
    <?php
        $q = "select booking.*, customer.customer_fname, customer.customer_lname from booking, customer where booking.customer_id=customer.customer_id";
        $result = mysqli_query($link, $q);
        while ($row = mysqli_fetch_array($result)){ 
            $total = $row['total_price'];
            $deposit = $row['deposit'];
            $remain = $total - $deposit;
            $q = "select min(booking_detail.date) as mindate, max(booking_detail.date) as maxdate, status_name, booking_detail.status_id from booking_detail, status where status.status_id=booking_detail.status_id and booking_id=".$row['booking_id'];
            $res = mysqli_query($link, $q);
            $r = mysqli_fetch_array($res);
            $cod = date ("Y-m-d", strtotime("+1 day", strtotime($r['maxdate'])));
            $status = $r['status_name'];
            if ($_st == $r['status_id']) {
    ?>
    <tr>
        <td><a href="main.php?booking_detail=true&booking_id=<?php echo $row['booking_id'];?>"><?php echo $row['booking_id'];?></a></td>
        <td><?php echo $row['customer_fname'].' '.$row['customer_lname'];?></td>
        <td><?php echo $row['booking_date'];?></td>
        <td><?php echo $r['mindate'];?></td>
        <td><?php echo $cod;?></td>
        <td><?php echo $total;?></td>
        <td><?php echo $deposit;?></td>
        <td><?php echo $row['deposit_date'];?></td>
        <td><?php echo $remain;?></td>
        <td><?php echo $status;?></td>
        <td><a href="main.php?booking=true&edit=true&booking_id=<?php echo $row['booking_id'];?>">edit</a></td>
    </tr>
    <?php
            }
        }
    ?>
    <tr>
        <td colspan="11" align="right"><a href="booking_report.php?status_id=<?php echo $_st ?>" target="_BLANK">Report</a></td>
    </tr>
</table>
<?php
    }
?>